package com.situ.achievementsystem.employee.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.situ.achievementsystem.employee.model.EmployeeModel;
import com.situ.achievementsystem.employee.service.EmployeeService;

/**
 * Servlet implementation class EmployeeServlet
 */
@WebServlet("/EmployeeServlet")
public class EmployeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmployeeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String action = request.getParameter("action");
		Object res = "";
		EmployeeModel model = null;
		switch (action) {
		case "reg":
//			String code = request.getParameter("code");
//			String name = request.getParameter("name");
//			String pass = request.getParameter("pass");
			model = new EmployeeModel(request.getParameter("code"),request.getParameter("name"),request.getParameter("pass"));
			res = service.insert(model);
//			break;
		case "login":
//			String code = request.getParameter("code");
//			String pass = request.getParameter("pass"); 
			model = new EmployeeModel(request.getParameter("code"),request.getParameter("pass"));
			res = service.login(model);
		    break;
		    
		case "add":
			model = new EmployeeModel(request.getParameter("code"),request.getParameter("name"));
			res = service.insert(model);
			break;
			
		case "list":
			model = new EmployeeModel(request.getParameter("code"),request.getParameter("name"),request.getParameter("pass"),request.getParameter("codeDept"),request.getParameter("nameDept"));
			res = service.selectList(model);
			res = new JSONArray((Collection<?>)res).toString();
			break;
		
		case "del":
			model = new EmployeeModel(request.getParameter("code"));
			res =service.delete(model);
			break;
		
		case "get":
			model = new EmployeeModel(request.getParameter("code"));
			res = service.selectModel(model);
			res = new JSONObject(res).toString();
			break;
		
		case "upd":
			model = new EmployeeModel(request.getParameter("code"),request.getParameter("name"));
			res = service.update(model);
			break;
			
		case "resetpass":
			model = new EmployeeModel(request.getParameter("code"));
			System.out.println(model);
			res =service.resetpass(model);
			break;
			
		case "updpass":
			model = new EmployeeModel(request.getParameter("code"),request.getParameter("pass"));
			res =service.updpass(model);
			break;
			
		case "updDept":
			model = new EmployeeModel(request.getParameter("code"),request.getParameter("codeDept"));
			res = service.updDept(model);
			break;
		
		default:
			break;
		}
		
		PrintWriter wr = response.getWriter();
		wr.print(res);
		wr.flush();
		wr.close();
		wr=null;
	}

//	private String reg(HttpServletRequest req) {
//		String code = req.getParameter("code");
//		String name = req.getParameter("name");
//		String pass = req.getParameter("pass");
//		EmployeeModel model = new EmployeeModel();
//		return service.insert(model);
//	}
	
	private EmployeeService service = new EmployeeService();
}
